package com.wwwarehouse.common.tools;


import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.wwwarehouse.common.R;
import com.wwwarehouse.common.common.Common;


/**
 * dialog工具类封装
 * Created by ShineF on 2017/6/19 0019.
 */

public class DialogTools {
    private static DialogTools mDialogTools;
    private Dialog mDialog;
    private ConfirmListener mListener;


    public static DialogTools getInstance() {
        if (null == mDialogTools) {
            synchronized (DialogTools.class) {
                if (null == mDialogTools) {
                    mDialogTools = new DialogTools();
                }
            }
        }

        return mDialogTools;
    }

    public interface ConfirmListener {
        void setConfirmListener(Dialog dialog, View view, String input);
    }

    public interface DismissListener {
        void setDismissListener(Dialog dialog, View view);
    }

    public interface OnDismissListener {
        void setDismissListener(Dialog dialog, View view);
    }

    public void setConfirmListener(ConfirmListener listener) {
        this.mListener = listener;
    }


    /**
     * 带取消确定的dialog
     *
     * @param context       上下文
     * @param title         标题
     * @param content       提示内容
     * @param cancelOutSide 点击窗口外面是否消失
     * @param listener      点击确定的回调, 如果是null则dismiss dialog
     */
    public void showCustomDialogPrompt(Context context, String title, String content, boolean cancelOutSide, final ConfirmListener listener) {
        View view = View.inflate(context, R.layout.view_custom_dialog, null);
        TextView contentText = (TextView) view.findViewById(R.id.tv_show_content);
        Button cancelBtn = (Button) view.findViewById(R.id.btn_cancel);
        TextView titleText = (TextView) view.findViewById(R.id.tv_title);
        Button confirmButton = (Button) view.findViewById(R.id.btn_confirm);
        if (TextUtils.isEmpty(content)) {
            contentText.setVisibility(View.GONE);
        } else {
            contentText.setText(content);

        }
        mDialog = new AlertDialog.Builder(context)
                .setView(view)
                .create();
        mDialog.setCanceledOnTouchOutside(cancelOutSide);
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
        WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
        params.width = context.getResources().getDisplayMetrics().widthPixels * 5 / 6;
        mDialog.getWindow().setAttributes(params);
        if (null != title) {
            titleText.setText(title);
        }
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });
        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.setConfirmListener(mDialog, v, "");
                } else {
                    mDialog.dismiss();
                }
            }
        });
    }


    /**
     * 带取消确定的dialog
     *
     * @param context       上下文
     * @param title         标题
     * @param content       提示内容
     * @param cancelOutSide 点击窗口外面是否消失
     * @param listener      点击确定的回调, 如果是null则dismiss dialog
     */
    public void showCustomDialogPrompt(Context context, String title, String content
            , boolean cancelOutSide, final ConfirmListener listener, String confirmText, String cancelText) {
        if (!Common.getInstance().isNotFastClick()) {
            return;
        }
        View view = View.inflate(context, R.layout.view_custom_dialog, null);
        TextView contentText = (TextView) view.findViewById(R.id.tv_show_content);
        Button cancelBtn = (Button) view.findViewById(R.id.btn_cancel);
        TextView titleText = (TextView) view.findViewById(R.id.tv_title);
        Button confirmButton = (Button) view.findViewById(R.id.btn_confirm);
        if (!TextUtils.isEmpty(confirmText)) {
            confirmButton.setText(confirmText);
        }
        if (!TextUtils.isEmpty(cancelText)) {
            cancelBtn.setText(cancelText);
        }
        if (TextUtils.isEmpty(content)) {
            contentText.setVisibility(View.GONE);
        } else {
            contentText.setText(content);

        }
        mDialog = new AlertDialog.Builder(context)
                .setView(view)
                .create();
        mDialog.setCanceledOnTouchOutside(cancelOutSide);
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
        WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
        params.width = context.getResources().getDisplayMetrics().widthPixels * 5 / 6;
        mDialog.getWindow().setAttributes(params);
        if (null != title) {
            titleText.setText(title);
        }
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });
        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.setConfirmListener(mDialog, v, "");
                } else {
                    mDialog.dismiss();
                }
            }
        });
    }

    /**
     * 带取消确定的dialog
     *
     * @param context       上下文
     * @param content       提示内容
     * @param cancelOutSide 点击窗口外面是否消失
     * @param listener      点击确定的回调, 如果是null则dismiss dialog
     */
    public void showCustomDialogPromptNoTitle(Context context, String content
            , boolean cancelOutSide, final ConfirmListener listener, String confirmText, String cancelText) {
        View view = View.inflate(context, R.layout.view_custom_dialog_no_title, null);
        TextView contentText = (TextView) view.findViewById(R.id.tv_show_content);
        Button cancelBtn = (Button) view.findViewById(R.id.btn_cancel);
        Button confirmButton = (Button) view.findViewById(R.id.btn_confirm);
        if (!TextUtils.isEmpty(confirmText)) {
            confirmButton.setText(confirmText);
        }
        if (!TextUtils.isEmpty(cancelText)) {
            cancelBtn.setText(cancelText);
        }
        if (TextUtils.isEmpty(content)) {
            contentText.setVisibility(View.GONE);
        } else {
            contentText.setText(content);

        }
        mDialog = new AlertDialog.Builder(context)
                .setView(view)
                .create();
        mDialog.setCanceledOnTouchOutside(cancelOutSide);
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
        WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
        params.width = context.getResources().getDisplayMetrics().widthPixels * 5 / 6;
        mDialog.getWindow().setAttributes(params);
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });
        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.setConfirmListener(mDialog, v, "");
                } else {
                    mDialog.dismiss();
                }
            }
        });
    }

    /**
     * 带取消确定的dialog
     *
     * @param context         上下文
     * @param content         提示内容
     * @param cancelOutSide   点击窗口外面是否消失
     * @param confirmListener 点击确定的回调, 如果是null则dismiss dialog
     * @param dismissListener 点击取消的回调, 如果是null则dismiss dialog
     */
    public void showCustomDialogPromptNoTitleCancleListen(Context context, String content, boolean cancelOutSide, final ConfirmListener confirmListener, final DismissListener dismissListener, String confirmText, String cancelText) {
        View view = View.inflate(context, R.layout.view_custom_dialog_no_title, null);
        TextView contentText = (TextView) view.findViewById(R.id.tv_show_content);
        Button cancelBtn = (Button) view.findViewById(R.id.btn_cancel);
        Button confirmButton = (Button) view.findViewById(R.id.btn_confirm);
        if (!TextUtils.isEmpty(confirmText)) {
            confirmButton.setText(confirmText);
        }
        if (!TextUtils.isEmpty(cancelText)) {
            cancelBtn.setText(cancelText);
        }
        if (TextUtils.isEmpty(content)) {
            contentText.setVisibility(View.GONE);
        } else {
            contentText.setText(content);

        }
        mDialog = new AlertDialog.Builder(context)
                .setView(view)
                .create();
        mDialog.setCanceledOnTouchOutside(cancelOutSide);
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
        WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
        params.width = context.getResources().getDisplayMetrics().widthPixels * 5 / 6;
        mDialog.getWindow().setAttributes(params);

        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null == dismissListener) {
                    mDialog.dismiss();
                } else {
                    dismissListener.setDismissListener(mDialog, v);
                }
            }
        });
        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != confirmListener) {
                    confirmListener.setConfirmListener(mDialog, v, "");
                } else {
                    mDialog.dismiss();
                }
            }
        });
    }

    /**
     * 带取消确定的dialog
     *
     * @param context         上下文
     * @param title           标题
     * @param content         提示内容
     * @param cancelOutSide   点击窗口外面是否消失
     * @param confirmListener 点击确定的回调, 如果是null则dismiss dialog
     * @param dismissListener 点击取消的回调, 如果是null则dismiss dialog
     */
    public void showCustomDialogPrompt(Context context, String title, String content, boolean cancelOutSide, final ConfirmListener confirmListener, final DismissListener dismissListener) {
        View view = View.inflate(context, R.layout.view_custom_dialog, null);
        TextView contentText = (TextView) view.findViewById(R.id.tv_show_content);
        Button cancelBtn = (Button) view.findViewById(R.id.btn_cancel);
        TextView titleText = (TextView) view.findViewById(R.id.tv_title);
        Button confirmButton = (Button) view.findViewById(R.id.btn_confirm);
        if (TextUtils.isEmpty(content)) {
            contentText.setVisibility(View.GONE);
        } else {
            contentText.setText(content);

        }
        if (TextUtils.isEmpty(title)) {
            titleText.setVisibility(View.GONE);
        }
        mDialog = new AlertDialog.Builder(context)
                .setView(view)
                .create();
        mDialog.setCanceledOnTouchOutside(cancelOutSide);
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
        WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
        params.width = context.getResources().getDisplayMetrics().widthPixels * 5 / 6;
        mDialog.getWindow().setAttributes(params);
        if (null != title) {
            titleText.setText(title);
        }
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null == dismissListener) {
                    mDialog.dismiss();
                } else {
                    dismissListener.setDismissListener(mDialog, v);
                }
            }
        });
        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != confirmListener) {
                    confirmListener.setConfirmListener(mDialog, v, "");
                } else {
                    mDialog.dismiss();
                }
            }
        });
    }


    /**
     * 带取消确定的dialog
     *
     * @param context         上下文
     * @param title           标题
     * @param content         提示内容
     * @param sureString      确定按钮文字
     * @param cancelString    取消按钮文字
     * @param confirmListener 点击确定的回调，如果是null则dismiss dialog
     * @param dismissListener 点击取消的回调，如果是null则dismiss dialog
     */
    public void showTCDialog(Context context, String title, String content, String sureString, String cancelString, final ConfirmListener confirmListener, final DismissListener dismissListener) {
        View view = View.inflate(context, R.layout.view_custom_dialog, null);
        TextView contentText = (TextView) view.findViewById(R.id.tv_show_content);
        Button cancelBtn = (Button) view.findViewById(R.id.btn_cancel);
        TextView titleText = (TextView) view.findViewById(R.id.tv_title);
        Button confirmButton = (Button) view.findViewById(R.id.btn_confirm);
        confirmButton.setText(sureString);
        cancelBtn.setText(cancelString);
        if (TextUtils.isEmpty(content)) {
            contentText.setVisibility(View.GONE);
        } else {
            contentText.setText(content);

        }
        if (TextUtils.isEmpty(title)) {
            titleText.setVisibility(View.GONE);
        }
        mDialog = new AlertDialog.Builder(context)
                .setView(view)
                .create();
        mDialog.setCanceledOnTouchOutside(true);
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
        WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
        params.width = context.getResources().getDisplayMetrics().widthPixels * 5 / 6;
        mDialog.getWindow().setAttributes(params);
        if (null != title) {
            titleText.setText(title);
        }
        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != confirmListener) {
                    confirmListener.setConfirmListener(mDialog, v, "");
                } else {
                    mDialog.dismiss();
                }
            }
        });
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null == dismissListener) {
                    mDialog.dismiss();
                } else {
                    dismissListener.setDismissListener(mDialog, v);
                }
            }
        });
    }

    /**
     * 带取消确定的dialog
     *
     * @param context         上下文
     * @param title           标题
     * @param content         提示内容
     * @param sureString      确定按钮文字
     * @param cancelString    取消按钮文字
     * @param confirmListener 点击确定的回调，如果是null则dismiss dialog
     * @param dismissListener 点击取消的回调，如果是null则dismiss dialog
     */
    public void showTCDialog(Context context, String title, String content, String sureString
            , String cancelString
            , String confirmText
            , String cancelText
            , final ConfirmListener confirmListener
            , final DismissListener dismissListener) {
        View view = View.inflate(context, R.layout.view_custom_dialog, null);
        TextView contentText = (TextView) view.findViewById(R.id.tv_show_content);
        Button cancelBtn = (Button) view.findViewById(R.id.btn_cancel);
        TextView titleText = (TextView) view.findViewById(R.id.tv_title);
        Button confirmButton = (Button) view.findViewById(R.id.btn_confirm);
        cancelBtn.setText(cancelText);
        confirmButton.setText(confirmText);
        confirmButton.setText(sureString);
        cancelBtn.setText(cancelString);
        if (TextUtils.isEmpty(content)) {
            contentText.setVisibility(View.GONE);
        } else {
            contentText.setText(content);

        }
        if (TextUtils.isEmpty(title)) {
            titleText.setVisibility(View.GONE);
        }
        ;
        mDialog = new AlertDialog.Builder(context)
                .setView(view)
                .create();
        mDialog.setCanceledOnTouchOutside(true);
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
        WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
        params.width = context.getResources().getDisplayMetrics().widthPixels * 5 / 6;
        mDialog.getWindow().setAttributes(params);
        if (null != title) {
            titleText.setText(title);
        }
        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != confirmListener) {
                    confirmListener.setConfirmListener(mDialog, v, "");
                } else {
                    mDialog.dismiss();
                }
            }
        });
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null == dismissListener) {
                    mDialog.dismiss();
                } else {
                    dismissListener.setDismissListener(mDialog, v);
                }
            }
        });
    }

    /**
     * 只有一个按钮的提示框
     *
     * @param context       上下文
     * @param title         标题
     * @param content       提示内容
     * @param cancelOutSide 点击外部是否消失
     * @param listener      按钮点击的回调事件, 传null默认dismiss dialog
     */
    public void showCustomWarning(Context context, String title, String content, boolean cancelOutSide, final ConfirmListener listener) {
        if (!Common.getInstance().isNotFastClick()) {
            return;
        }
        View view = View.inflate(context, R.layout.view_custom_2_dialog, null);
        TextView contentText = (TextView) view.findViewById(R.id.tv_show_content);
        TextView titleText = (TextView) view.findViewById(R.id.tv_title);
        Button confirmButton = (Button) view.findViewById(R.id.btn_confirm);
        contentText.setText(content);
        mDialog = new AlertDialog.Builder(context)
                .setView(view)
                .create();
        mDialog.setCanceledOnTouchOutside(cancelOutSide);
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
        WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
        params.width = context.getResources().getDisplayMetrics().widthPixels * 5 / 6;
        mDialog.getWindow().setAttributes(params);
        if (null != title) {
            titleText.setText(title);
        }
        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.setConfirmListener(mDialog, v, "");
                } else {
                    mDialog.dismiss();
                }
            }
        });
    }

    /**
     * 只有一个按钮的提示框
     *
     * @param context       上下文
     * @param title         标题
     * @param content       提示内容
     * @param button        按钮内容
     * @param cancelOutSide 点击外部是否消失
     * @param listener      按钮点击的回调事件, 传null默认dismiss dialog
     */
    public void showCustomWarning(Context context, String title, String content, String button, boolean cancelOutSide, final ConfirmListener listener) {
        if (!Common.getInstance().isNotFastClick()) {
            return;
        }
        View view = View.inflate(context, R.layout.view_custom_2_dialog, null);
        TextView contentText = (TextView) view.findViewById(R.id.tv_show_content);
        TextView titleText = (TextView) view.findViewById(R.id.tv_title);
        Button confirmButton = (Button) view.findViewById(R.id.btn_confirm);
        if (title.isEmpty()) {
            titleText.setVisibility(View.GONE);
        } else {
            titleText.setVisibility(View.VISIBLE);
            titleText.setText(title);
        }
        contentText.setText(content);
        confirmButton.setText(button);
        mDialog = new AlertDialog.Builder(context)
                .setView(view)
                .create();
        mDialog.setCanceledOnTouchOutside(cancelOutSide);
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
        WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
        params.width = context.getResources().getDisplayMetrics().widthPixels * 5 / 6;
        mDialog.getWindow().setAttributes(params);
        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.setConfirmListener(mDialog, v, "");
                } else {
                    mDialog.dismiss();
                }
            }
        });
    }

    /**
     * 只有一个按钮的提示框
     *
     * @param context       上下文
     * @param title         标题
     * @param content       提示内容
     * @param button        按钮内容
     * @param cancelOutSide 点击外部是否消失
     * @param listener      按钮点击的回调事件, 传null默认dismiss dialog
     */
    public void showCustomWarning(Context context, String title, String content, String button, boolean cancelOutSide
            , final OnDismissListener dismissListener, final ConfirmListener listener) {
        if (!Common.getInstance().isNotFastClick()) {
            return;
        }
        final View view = View.inflate(context, R.layout.view_custom_2_dialog, null);
        TextView contentText = (TextView) view.findViewById(R.id.tv_show_content);
        TextView titleText = (TextView) view.findViewById(R.id.tv_title);
        Button confirmButton = (Button) view.findViewById(R.id.btn_confirm);
        if (title.isEmpty()) {
            titleText.setVisibility(View.GONE);
        } else {
            titleText.setVisibility(View.VISIBLE);
            titleText.setText(title);
        }
        contentText.setText(content);
        confirmButton.setText(button);
        mDialog = new AlertDialog.Builder(context)
                .setView(view)
                .create();
        mDialog.setCanceledOnTouchOutside(cancelOutSide);
        if (!mDialog.isShowing()) {
            mDialog.show();
        }

        mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                dismissListener.setDismissListener(mDialog, view);
            }
        });
        WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
        params.width = context.getResources().getDisplayMetrics().widthPixels * 5 / 6;
        mDialog.getWindow().setAttributes(params);
        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.setConfirmListener(mDialog, v, "");
                } else {
                    mDialog.dismiss();
                }
            }
        });
    }

    /**
     * 只有一个按钮的提示框
     *
     * @param context       上下文
     * @param content       提示内容
     * @param button        按钮内容
     * @param cancelOutSide 点击外部是否消失
     * @param listener      按钮点击的回调事件, 传null默认dismiss dialog
     */
    public void showCustomWarningNoTitle(Context context, String content, String button, boolean cancelOutSide, final ConfirmListener listener) {
        if (!Common.getInstance().isNotFastClick()) {
            return;
        }
        View view = View.inflate(context, R.layout.view_custom_4_dialog, null);
        TextView contentText = (TextView) view.findViewById(R.id.tv_show_content);
        Button confirmButton = (Button) view.findViewById(R.id.btn_confirm);
        contentText.setText(content);
        confirmButton.setText(button);
        mDialog = new AlertDialog.Builder(context)
                .setView(view)
                .create();
        mDialog.setCanceledOnTouchOutside(cancelOutSide);
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
        WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
        params.width = context.getResources().getDisplayMetrics().widthPixels * 5 / 6;
        mDialog.getWindow().setAttributes(params);
        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.setConfirmListener(mDialog, v, "");
                } else {
                    mDialog.dismiss();
                }
            }
        });
    }

    /**
     * 带有输入框的Dialog
     *
     * @param context       上下文
     * @param title         标题
     * @param cancelOutSide 点击外面是否消失
     * @param listener      按钮监听
     */
    public void showCustomInput(Context context, String title, boolean cancelOutSide, final ConfirmListener listener) {
        if (!Common.getInstance().isNotFastClick()) {
            return;
        }
        View view = View.inflate(context, R.layout.view_custom_3_dialog, null);
        Button cancelBtn = (Button) view.findViewById(R.id.btn_cancel);
        Button confirmButton = (Button) view.findViewById(R.id.btn_confirm);
        TextView titleText = (TextView) view.findViewById(R.id.tv_title);
        final EditText etInput = (EditText) view.findViewById(R.id.et_input);
        mDialog = new AlertDialog.Builder(context)
                .setView(view)
                .create();
        mDialog.setCanceledOnTouchOutside(cancelOutSide);
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
        WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
        params.width = context.getResources().getDisplayMetrics().widthPixels * 5 / 6;
        mDialog.getWindow().setAttributes(params);
        if (null != title) {
            titleText.setText(title);
        }
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });
        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    String inputContent = etInput.getText().toString().trim();
                    listener.setConfirmListener(mDialog, v, inputContent);
                } else {
                    mDialog.dismiss();
                }
            }
        });
    }

    /**
     * 倒计时对话框
     *
     * @param context       上下文
     * @param title         标题
     * @param content       提示内容
     * @param time          倒计时时间
     * @param cancelOutSide 点击外部是否消失
     */
    public void showCustomTiming(Context context, String title, String content, final long time, boolean cancelOutSide) {
        View view = View.inflate(context, R.layout.view_custom_timing_dialog, null);
        TextView titleText = (TextView) view.findViewById(R.id.tv_title);
        TextView contentText = (TextView) view.findViewById(R.id.tv_content);
        final TextView timeText = (TextView) view.findViewById(R.id.tv_time);

        mDialog = new AlertDialog.Builder(context)
                .setView(view)
                .create();
        mDialog.setCanceledOnTouchOutside(cancelOutSide);

        if (!mDialog.isShowing()) {
            mDialog.show();
        }

        WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
        params.width = context.getResources().getDisplayMetrics().widthPixels * 5 / 6;
        mDialog.getWindow().setAttributes(params);

        if (null != title) {
            titleText.setText(title);
        }

        if (null != content) {
            contentText.setText(content);
        }

        //倒计时
        CountDownTimer countDownTimer = new CountDownTimer(time * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                timeText.setText(millisUntilFinished / 1000 + "S");
            }

            @Override
            public void onFinish() {
                if (mDialog != null && mDialog.isShowing()) {
                    mDialog.dismiss();
                }
            }
        };

        countDownTimer.start();
    }

    /**
     * 指纹识别对话框
     *
     * @param context       上下文
     * @param cancelOutSide 点击外部是否消失
     */
    public void showCustomStartFinger(Context context, boolean cancelOutSide, final ConfirmListener listener, final DismissListener dismissListener) {
        showCustomStartFinger(context, cancelOutSide, null, listener, dismissListener);
    }

    /**
     * 指纹识别对话框
     *
     * @param context       上下文
     * @param cancelOutSide 点击外部是否消失
     */
    public void showCustomStartFinger(Context context, boolean cancelOutSide, String content, final ConfirmListener listener, final DismissListener dismissListener) {
        View view = View.inflate(context, R.layout.view_custom_finger_start_dialog, null);
        Button cancelBtn = (Button) view.findViewById(R.id.btn_cancel);
        TextView tvFinger = (TextView) view.findViewById(R.id.tv_content_finger);
        if (!TextUtils.isEmpty(content)) {
            tvFinger.setText(content);
        }
        mDialog = new AlertDialog.Builder(context)
                .setView(view)
                .create();
        mDialog.setCanceledOnTouchOutside(cancelOutSide);
        if (!mDialog.isShowing()) {
            mDialog.show();

            if (null != listener) {
                listener.setConfirmListener(mDialog, view, "");
            }
        }

        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null == dismissListener) {
                    mDialog.dismiss();
                } else {
                    dismissListener.setDismissListener(mDialog, v);
                }
            }
        });
        WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
        params.width = context.getResources().getDisplayMetrics().widthPixels * 5 / 6;
        mDialog.getWindow().setAttributes(params);
    }

    /**
     * 指纹识别对话框
     *
     * @param context       上下文
     * @param cancelOutSide 点击外部是否消失
     */
    public void showCustomFinger(Context context, boolean cancelOutSide, Bitmap bitmap, String content, String button, final ConfirmListener listener, final DismissListener dismissListener) {
        if (!Common.getInstance().isNotFastClick()) {
            return;
        }
        View view = View.inflate(context, R.layout.view_custom_finger_start_dialog, null);
        ImageView ivFinger = (ImageView) view.findViewById(R.id.iv);
        TextView tvFinger = (TextView) view.findViewById(R.id.tv_content_finger);
        Button cancelBtn = (Button) view.findViewById(R.id.btn_cancel);
        if (bitmap != null) {
            ivFinger.setImageBitmap(bitmap);
        }
        if (!TextUtils.isEmpty(content)) {
            tvFinger.setText(content);
        }
        if (!TextUtils.isEmpty(button)) {
            cancelBtn.setText(button);
        }
        mDialog = new AlertDialog.Builder(context)
                .setView(view)
                .create();
        mDialog.setCanceledOnTouchOutside(cancelOutSide);
        if (!mDialog.isShowing()) {
            mDialog.show();

            if (null != listener) {
                listener.setConfirmListener(mDialog, view, "");
            }
        }

        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null == dismissListener) {
                    mDialog.dismiss();
                } else {
                    dismissListener.setDismissListener(mDialog, v);
                }
            }
        });
        WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
        params.width = context.getResources().getDisplayMetrics().widthPixels * 5 / 6;
        mDialog.getWindow().setAttributes(params);

    }

    /**
     * 带取消确定的dialog
     *
     * @param cancelOutSide 点击窗口外面是否消失
     * @param listener      点击确定的回调, 如果是null则dismiss dialog
     */
    public void showConfirmCloseFinger(Context context, boolean cancelOutSide, final ConfirmListener listener) {
        if (!Common.getInstance().isNotFastClick()) {
            return;
        }
        View view = View.inflate(context, R.layout.view_custom_finger_close_dialog, null);
        TextView contentText = (TextView) view.findViewById(R.id.tv_show_content);
        Button cancelBtn = (Button) view.findViewById(R.id.btn_cancel);
        Button confirmButton = (Button) view.findViewById(R.id.btn_confirm);
        mDialog = new AlertDialog.Builder(context)
                .setView(view)
                .create();
        mDialog.setCanceledOnTouchOutside(cancelOutSide);
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
        WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
        params.width = context.getResources().getDisplayMetrics().widthPixels * 5 / 6;
        mDialog.getWindow().setAttributes(params);

        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });
        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.setConfirmListener(mDialog, v, "");
                } else {
                    mDialog.dismiss();
                }
            }
        });
    }
}
